package lanxing.future.lamda;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class ForEach {
    public static void main(String[] args) {

        List<String> strs = new ArrayList<String>();
        strs.add("a");
        strs.add("b");
        strs.add("c");

        strs.forEach(s -> {
            if (s.equals("a")) {
                System.out.println("is a");
            } else {
                System.out.println("not a");
            }
        });

        boolean result = strs.stream().allMatch(s -> {
            return s.equals("a");
        });

        boolean result2 = strs.stream().anyMatch(s -> {
            return s.equals("b");
        });
        System.out.println(result + "," + result2);

        List<String> list = strs.stream().filter(s -> {
            return !s.equals("a");
        }).collect(Collectors.toList());
        
        //to ask o 为什么要这样写
        List<String> list2 = strs.stream().filter(s ->{
            return s.equals("s");
        }).collect(Collectors.toList());
        
        
        
        

        System.out.println(list);
        
        
        List<Person> persons = new ArrayList<Person>();
        
        persons.add(new Person("上海","兰星"));

        persons.add(new Person("上海","兰星"));
        persons.add(new Person("上海","兰星0"));
        persons.add(new Person("广州","兰星2"));
        persons.add(new Person("北方","兰星3"));
        persons.add(new Person("北方","兰星5"));
        persons.add(new Person("美国","兰星4"));
        
        Map<String, List<Person>> cityGroup = persons.stream().collect(Collectors.groupingBy(Person::getCity));
        
         List<Person> teset = cityGroup.get("ses");
         System.out.println(teset==null);
        
        System.out.println(cityGroup);
    }
    
    
}
class Person{
    String city;
    String name;
    
    public Person() {
        
    }
    
    public Person(String city,String name) {
        this.city = city;
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}